package de.dhbw.vs.permachat.client.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.fusesource.restygwt.client.MethodCallback;
import org.fusesource.restygwt.client.Resource;
import org.fusesource.restygwt.client.RestService;
import org.fusesource.restygwt.client.RestServiceProxy;

import com.google.gwt.core.client.GWT;

import de.dhbw.vs.permachat.client.dto.ChatMessage;
import de.dhbw.vs.permachat.client.dto.ChatRoom;

/**
 * Asynchrones RestyGWT Interface für den ChatService.
 * 
 * @author flo
 * 
 */
// Path defined at Creator class, see below
// @Path("/chat/rooms")
@Produces(MediaType.APPLICATION_JSON)
public interface ChatServiceAsync extends RestService {

	@GET
	public void listChatRooms(MethodCallback<List<ChatRoom>> pCallback);
	
	@GET
	public void listChatRooms(@QueryParam("latitude") double pLatitude, @QueryParam("longitude") double pLongitude, MethodCallback<List<ChatRoom>> pCallback);

	@GET
	@Path("{roomName}/messages")
	public void listChatMessages(@PathParam("roomName") String pChatRoom,
			@QueryParam("count") Integer pCount,
			@QueryParam("lastId") Long pLastId,
			MethodCallback<List<ChatMessage>> pCallback);

	@POST
	@Path("{roomName}/messages")
	@Consumes(MediaType.APPLICATION_JSON)
	public void postChatMessage(@PathParam("roomName") String pChatRoom,
			ChatMessage pMessage, MethodCallback<Void> pCallback);

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public void createChatRoom(ChatRoom pChatRoom,
			MethodCallback<Void> pCallback);

	@GET
	@Path("nearest")
	public void getNearestChatRoom(@QueryParam("latitude") double pLatitude,
			@QueryParam("longitude") double pLongitude,
			MethodCallback<ChatRoom> pCallback);

	/**
	 * Utility class to get the instance of the Rest Service See
	 * http://blog.javaforge.net/post/30469901979/gwt-rest
	 */
	public static final class Creator {

		private static ChatServiceAsync instance;

		public static final ChatServiceAsync get() {
			if (instance == null) {
				instance = GWT.create(ChatServiceAsync.class);
				((RestServiceProxy) instance).setResource(new Resource(
						GWT.getHostPageBaseURL() + "rest/chat/rooms"));
			}
			return instance;
		}

		private Creator() {
			// Utility class should not be instantiated
		}
	}
}
